package com.zhgxi.zf.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slave4j.orm.Compositor;
import org.slave4j.orm.Filtration;
import org.slave4j.orm.Compositor.CompositorType;
import org.slave4j.orm.Filtration.MatchType;
import org.slave4j.orm.hibernate.BaseDao;
import org.slave4j.orm.hibernate.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhgxi.zf.entity.Notice;

@Service
@Transactional
public class NoticeService extends BaseService<Notice> {
	@Override
	@Resource(name = "noticeDao")
	public void setBaseDao(BaseDao<Notice> baseDao) {
		this.baseDao = baseDao;
	}

	public List<Notice> findAllNotice() {
		Filtration visible = new Filtration(MatchType.EQ, true, "visible");
		Compositor compositor = new Compositor("ndate", CompositorType.DESC);
		return baseDao.findList(compositor,visible);
	}

	public List<Notice> findByDate(Date d) {
		Filtration visible = new Filtration(MatchType.EQ, true, "visible");
		Filtration date = new Filtration(MatchType.EQ, d, "ndate");
		return baseDao.findList(visible,date);
	}
	
	public boolean deleteNotice(Notice no) {
		try {
			baseDao.unVisible(no.getId());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean saveNotice(Notice no) {
		try {
			if (no.isNew() || no.getId() == 0) {
				baseDao.save(no);
			} else {
				baseDao.update(no);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
